var mongoose = require('./MongooseDB');
var ResourceAccess = require('./ResourceAccess');
var FeatureParser = require('./FeatureParser');

// FeatureParser("2019年8月11日中国东海台风利奇马运动轨迹图", function(err, data){
//     if(err){
//         console.log(err);
//     }
//     else{
//         console.log(data);
//     }
// });

mongoose.dbinit('172.21.213.105', 27017, 'UbiquitousMap');

var tablenNames = [
    {table : 'OSM_ContentTag', field : [ 'properties', 'name' ]}
]

var cb = function(index){
    count ++;
    return function(err, docs){
        count --;
        if (count == 0){
            
        }
        if(err){
            console.log(err);
        }
        for (var i = 0; i < docs.length; i++){
            var value = docs[i];
            for(var j = 0 ; j < tablenNames[index].field.length; j++){
                value = value[tablenNames[index].field[j]];
            }
            // console.log(value);
            if(value.trim() != ''){
                FeatureParser(value, {
                    _id : docs[i]._id,
                    Collection : tablenNames[index].table,
                    MetaID : docs[i].meta
                }, function(err, data){
                    if(err){
                       console.log(err); 
                    }
                    else{
                        for (var i = 0; i < data.Time.length; i++){
                            var geoFeature = ResourceAccess.CreateGeoFeature();
                            geoFeature.MetaID = data.meta;

                            geoFeature.ConfigureTime(data.TagType, data.TagId, data.Time[i]);

                            ResourceAccess.InsertGeoFeature(geoFeature, function(err, result){
                                if (err){
                                    return console.log(err);
                                }
                            });
                        }
                        for (var i = 0; i < data.Place.length; i++){
                            var geoFeature = ResourceAccess.CreateGeoFeature();
                            geoFeature.MetaID = data.meta;

                            geoFeature.ConfigurePlace(data.TagType, data.TagId, data.Place[i]);

                            ResourceAccess.InsertGeoFeature(geoFeature, function(err, result){
                                if (err){
                                    return console.log(err);
                                }
                            });
                        }
                        for (var i = 0; i < data.Person.length; i++){
                            var geoFeature = ResourceAccess.CreateGeoFeature();
                            geoFeature.MetaID = data.meta;

                            geoFeature.ConfigurePerson(data.TagType, data.TagId, data.Person[i]);

                            ResourceAccess.InsertGeoFeature(geoFeature, function(err, result){
                                if (err){
                                    return console.log(err);
                                }
                            });
                        }
                        for (var i = 0; i < data.Thing.length; i++){
                            var geoFeature = ResourceAccess.CreateGeoFeature();
                            geoFeature.MetaID = data.meta;

                            geoFeature.ConfigureThing(data.TagType, data.TagId, data.Thing[i]);

                            ResourceAccess.InsertGeoFeature(geoFeature, function(err, result){
                                if (err){
                                    return console.log(err);
                                }
                            });
                            
                        }
                        for (var i = 0; i < data.Event.length; i++){
                            var geoFeature = ResourceAccess.CreateGeoFeature();
                            geoFeature.MetaID = data.meta;

                            geoFeature.ConfigureEvent(data.TagType, data.TagId, data.Event[i]);

                            ResourceAccess.InsertGeoFeature(geoFeature, function(err, result){
                                if (err){
                                    return console.log(err);
                                }
                            });
                            
                        }
                        for (var i = 0; i < data.Phenomenon.length; i++){
                            var geoFeature = ResourceAccess.CreateGeoFeature();
                            geoFeature.MetaID = data.meta;

                            geoFeature.ConfigurePhenomenon(data.TagType, data.TagId, data.Phenomenon[i]);

                            ResourceAccess.InsertGeoFeature(geoFeature, function(err, result){
                                if (err){
                                    return console.log(err);
                                }
                            });
                            
                        }
                        for (var i = 0; i < data.Scene.length; i++){
                            var geoFeature = ResourceAccess.CreateGeoFeature();
                            geoFeature.MetaID = data.meta;

                            geoFeature.ConfigureScene(data.TagType, data.TagId, data.Scene[i]);

                            ResourceAccess.InsertGeoFeature(geoFeature, function(err, result){
                                if (err){
                                    return console.log(err);
                                }
                            });
                            
                        }
                    }
                });
            }
        }
    }
}

var count = 0;

for (var index_table = 0; index_table < tablenNames.length; index_table++){
    ResourceAccess.BindTagCollection(tablenNames[index_table].table);
    ResourceAccess.GetAllTagDocument(cb(index_table));
}

// var geoFeature = ResourceAccess.CreateGeoFeature();
// geoFeature.ConfigureEvent("XXXX_Legend", "key", "hahaha");

// ResourceAccess.InsertGeoFeature(geoFeature, function(err, result){
//     if (err){
//         return console.log(err);
//     }
// });